/******************** (C) COPYRIGHT 2014 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2014/01
* AUTHOR:				SA1
* IC:						SN32F240
* DESCRIPTION:	
*____________________________________________________________________________
* REVISION	Date				User		Description
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include	"SN32F280.h"
#include	"SN32F200_Def.h"
#include  "I2C.h"
#include	"Utility.h"
#include "..\Project\UserSet.h"
/*_____ D E C L A R A T I O N S ____________________________________________*/
uint8_t hwI2C_Fifo[I2C_FIFO_LEN];

/*_____ D E F I N I T I O N S ______________________________________________*/


/*_____ M A C R O S ________________________________________________________*/
#define			I2C_DELAY			UT_Delay_Nus(6)

/*_____ F U N C T I O N S __________________________________________________*/

/*****************************************************************************
* Function		: WT_I2C_1Byte
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void WT_I2C_1Byte(uint8_t Device_Addr,uint8_t WR_Addr,uint8_t data)
{
	I2C_Start();
	WT_Byte(Device_Addr);
	if(I2C_CACK() == 0)
	{
		WT_Byte(WR_Addr);
		if(I2C_CACK() == 0)
		{
			WT_Byte(data);
			I2C_CACK();
		}
	}
	I2C_Stop();
}

/*****************************************************************************
* Function		: WT_I2C_1Byte
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void WT_I2C_NByte_Const(uint8_t Device_Addr,uint8_t WR_Addr,uint8_t data,uint8_t len)
{
	uint8_t i;
	I2C_Start();
	WT_Byte(Device_Addr);
	if(I2C_CACK() == 0)
	{
		WT_Byte(WR_Addr);
		if(I2C_CACK() == 0)
		{
			for(i=0;i<len;i++)
			{
				WT_Byte(data);
				if(I2C_CACK() == 1)
					break;			
			}
		}
	}
	I2C_Stop();
}

/*****************************************************************************
* Function		: WT_I2C_1Byte
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void WT_I2C_NByte_Val(uint8_t Device_Addr,uint8_t WR_Addr,uint8_t len)
{
	uint8_t i;
	I2C_Start();
	WT_Byte(Device_Addr);
	if(I2C_CACK() == 0)
	{
		WT_Byte(WR_Addr);
		if(I2C_CACK() == 0)
		{
			for(i=0;i<len;i++)
			{
				WT_Byte(hwI2C_Fifo[i]);
				if(I2C_CACK() == 1)
					break;			
			}
		}
	}
	I2C_Stop();
}

/*****************************************************************************
* Function		: RD_I2C_NByte_Val
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void RD_I2C_NByte_Val(uint8_t Device_Addr,uint8_t WR_Addr,uint8_t len)
{
	uint8_t i;
	I2C_Start();
	WT_Byte(Device_Addr);
	if(I2C_CACK() == 0)
	{
		WT_Byte(WR_Addr);
		if(I2C_CACK() == 0)
		{
			I2C_Start();
			WT_Byte(Device_Addr|0x01);
			if(I2C_CACK() == 0)	
			{
				for(i=0;i<len;i++)
				{
					hwI2C_Fifo[i] = RD_Byte();
					I2C_MACK();		
				}			
			}				
		}
	}
	I2C_Stop();
}

/*****************************************************************************
* Function		: I2C_Start
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void I2C_Start(void)
{
	SCL_HIGH;
	I2C_DELAY;
	SDA_OUTPUT;
	SDA_HIGH;
	I2C_DELAY;
	SDA_LOW;
	I2C_DELAY;
	SCL_LOW;
}
/*****************************************************************************
* Function		: I2C_Stop
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void I2C_Stop(void)
{
	SDA_OUTPUT;
	SDA_LOW;
	I2C_DELAY;
	SCL_HIGH;
	I2C_DELAY;
	SDA_HIGH;
	I2C_DELAY;
}

/*****************************************************************************
* Function		: I2C_CACK
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
uint8_t I2C_CACK(void)
{
	uint8_t i = 0;;
	
	SDA_HIGH;
	SDA_INPUT;
	I2C_DELAY;
	SCL_HIGH;
	I2C_DELAY;
	if(SDA_STATUS)
		i = 1;
	SCL_LOW;
	return i;
}

/*****************************************************************************
* Function		: I2C_MNACK
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void I2C_MNACK(void)
{
	SDA_OUTPUT;
	SDA_HIGH;
	I2C_DELAY;
	SCL_HIGH;
	I2C_DELAY;
	I2C_DELAY;
	SCL_LOW;
	SDA_LOW;
}

/*****************************************************************************
* Function		: I2C_MACK
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void I2C_MACK(void)
{
	SDA_OUTPUT;
	SDA_LOW;	
	I2C_DELAY;
	SCL_HIGH;
	I2C_DELAY;
	SCL_LOW;
	SDA_HIGH;
	I2C_DELAY;
}
/*****************************************************************************
* Function		: WT_Byte
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void WT_Byte(uint8_t data)
{
	uint8_t i;
	SDA_OUTPUT;
	for(i=0;i<8;i++)
	{
		SCL_LOW;
		if(data&0x80)
			SDA_HIGH;
		else
			SDA_LOW;
		I2C_DELAY;
		SCL_HIGH;
		I2C_DELAY;
		data <<= 1;
	}	
	SCL_LOW;
}
/*****************************************************************************
* Function		: WT_Byte
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
uint8_t RD_Byte(void)
{
	uint8_t i,data=0;
	SDA_INPUT;
	for(i=0;i<8;i++)
	{
		data <<= 1;
		SCL_LOW;	
		I2C_DELAY;
		SCL_HIGH;	
		I2C_DELAY;
		if(SDA_STATUS)
			data |= 0x01;
	}
		SCL_LOW;	
	return data;
}
